package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.mapper.TBUserMapper;
import com.qf.pojo.TBUser;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<TBUserMapper, TBUser> implements UserDetailsService, UserService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private TBUserMapper mapper;

    /**
     * 认证方法
     * @param username 前台传入的用户名
     * @return 认证对象
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

//        //如果用户名是 admin123，那么就代表用户名是正确的
//        if ("admin123".equals(username)){
//            System.out.println("-------------用户名admin123存在-------------");
//            String encode = passwordEncoder.encode("123456");
//            //权限列表
//            List<GrantedAuthority> list = new ArrayList<>();
//            return new User(username,encode,list);
//        }
//        System.out.println("-----------用户名不存在---------");
//        return null;

        //连接数据库
        //条件查询
        QueryWrapper<TBUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        TBUser tbUser = mapper.selectOne(queryWrapper);
        if (tbUser != null){
            //需要去数据库查询 角色  权限等数据
            // GrantedAuthority  代表角色和权限
            List<GrantedAuthority> list = new ArrayList<>();
            System.out.println("用户存在");
            if("zhangsan".equals(tbUser.getUsername())){
                //凡是角色 必须添加  ROLE_
                list.add(new SimpleGrantedAuthority("ROLE_student"));
            }else if ("ww".equals(tbUser.getUsername())){
                list.add(new SimpleGrantedAuthority("update"));//凡是不是ROLE开头的就是权限
            }else if ("lisi".equals(tbUser.getUsername())){
                list.add(new SimpleGrantedAuthority("ROLE_teacher"));
            }else if ("zl".equals(tbUser.getUsername())){
                list.add(new SimpleGrantedAuthority("teacher_delete")); //里面好像只能写一个，不能写多个
            }

            User user = new User(tbUser.getUsername(),tbUser.getPassword(),list);
            return user;
        }
        System.out.println("用户不存在");
        return null;

    }

}